package com.opensns.android;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

import com.sns.R;

public class OauthDialog extends Dialog {

	public OauthDialog(Context context, String authUrl, OauthCallbackHandler handler) {
		super(context, R.style.OauthDialog);
		
		mHandler = handler;
		mProgress = new ProgressDialog(context);
		mProgress.setMessage("正在加载授权页面...");
		mWebView = new WebView(getContext());
		mWebView.setScrollBarStyle(0);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new AuthorizeWebViewClient());
        mWebView.setVisibility(View.INVISIBLE);
        mLayout = new RelativeLayout(getContext());
        mLayout.addView(mWebView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        setContentView(mLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mWebView.loadUrl(authUrl);
	}
	
	private void handleResponse(WebView view, String url) {
		mHandler.handleResponse(url);
        dismiss();
	}
	
	private WebView mWebView;
	private RelativeLayout mLayout;
	private ProgressDialog mProgress;
	private OauthCallbackHandler mHandler;
	
	private class AuthorizeWebViewClient extends WebViewClient {
		
		private final String tag = "OauthWebView";
		
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.d(tag, "Redirect: " + url);
			if (url.startsWith(mHandler.getCallbackUrl())) {
				handleResponse(view, url);
			}
			view.loadUrl(url);
			return true;
		}
		
		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			Log.d(tag, "Page start: " + url);
			if (url.startsWith(mHandler.getCallbackUrl())) {
				Log.d(tag, "Callback: " + mHandler.getCallbackUrl());
				handleResponse(view, url);
				view.stopLoading();
			} else {
				super.onPageStarted(view, url, favicon);
				mProgress.show();
			}
		}
		
		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			mProgress.dismiss();
			mWebView.setVisibility(View.VISIBLE);
		}
		
		@Override
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			Log.d(tag, "Received error: " + String.valueOf(errorCode));
			String msg = String.format("WebClient Error: code=%d desc=%s url=%s", errorCode, description, failingUrl);
			mHandler.onError(new Exception(msg));
			dismiss();			
		}
		
		public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

	}
	
	public interface OauthCallbackHandler {
		
		public String getCallbackUrl();
		
		public void handleResponse(String callbackUrl);
		
		public void onError(Exception e);
	}
}
